/**
 * 
 */
package component.activemq;

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 
 * @author liuboen
 * 
 * since:2012-6-7
 * version:1.0.0
 */
public class TopicPersistentPublish {

	/**
	 * @param args
	 * @throws JMSException
	 */
	public static void main(String[] args) throws JMSException {
		// ConnectionFactory ：连接工厂，JMS 用它创建连接
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
		TopicConnection topicConnection = connectionFactory.createTopicConnection();  

		TopicSession session = topicConnection.createTopicSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
		Topic topic = session.createTopic("publish-topic");

		TopicPublisher publisher = session.createPublisher(topic);
		// 设置订阅模式为持久化
		publisher.setDeliveryMode(DeliveryMode.PERSISTENT);
		// 设置持久化时间，默认为永不过期，方法参数为毫秒
		publisher.setTimeToLive(60000);

		// 创建一条文本消息
		MapMessage message = session.createMapMessage();
		message.setBoolean("bl", Boolean.TRUE);
		message.setString("tst", "cccc");
		publisher.publish(message);
		
		session.commit();
		topicConnection.start();
		
		publisher.close();
		session.close();
		topicConnection.close();

	}

}
